package controllers

import (
	"github.com/xiaomeng79/go-common-tools/models"
	"encoding/json"
	"strconv"
	"github.com/xiaomeng79/go-common-tools/libs"
)

// 定义发送邮件的服务器和发送邮件
type EmailController struct {
	BaseController
}



// @Title 添加发送邮件的服务器
// @Description 添加发送邮件的服务器
// @Param	body		body 	models.ReqEmail	true		"添加邮件服务器需要添加的参数，端口默认：465,"IsSsl":为是否是ssl协议，1为是，0为否,一般填 1 "HostName": "smtp.mxhichina.com" 邮箱服务器地址，一般填这个"
// @Success 0 {int64} id
// @Failure -1 参数错误,具体看message
// @router / [post]
func (this *EmailController) Post() {
	var email models.Email
	json.Unmarshal(this.Ctx.Input.RequestBody, &email)
	id,err := email.AddEmail()
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,map[string]int64{"id": id})
	}
	this.ajaxMsg("ok",MSG_OK,map[string]int64{"id": id})
}

// @Title 获取发送邮件服务器的信息
// @Description 获取发送邮件服务器的信息
// @Param	id		path 	int64	true		"根据ID获取邮件服务器的信息"
// @Success 0 {object} models.ReqEmail
// @Failure -1 获取失败
// @router /:id [get]
func (this *EmailController) Get() {
	Id := this.Ctx.Input.Param(":id")
	if Id == "" {
		this.ajaxMsg("id不能为空",MSG_ERR,"")
	}
	var email models.Email
	id, _ := strconv.ParseInt(Id,10,64)
	//var reqEmail models.ReqEmail
	data,err:= email.GetEmail(id)
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	this.ajaxMsg("ok",MSG_OK,data)

}

// @Title 获取所有邮件服务器的信息
// @Description 获取所有邮件服务器的信息
// @Success 0 {object} []models.ReqEmail
// @Failure -1 获取失败
// @router / [get]
func (this *EmailController) GetAll() {
	var email models.Email
	data,err:= email.GetAllEmail()
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	this.ajaxMsg("ok",MSG_OK,data)
}

// @Title 更新一个邮件服务器的信息
// @Description 更新一个邮件服务器的信息
// @Param	id		path 	string	true		"邮件服务器的ID"
// @Param	body		body 	models.ReqEmail	true		"跟新的内容"
// @Success 0 {object} models.ReqEmail
// @Failure -1 更新失败
// @router /:id [put]
func (this *EmailController) Put() {
	Id := this.Ctx.Input.Param(":id")
	if Id == "" {
		this.ajaxMsg("id不能为空",MSG_ERR,"")
	}
	var email models.Email
	json.Unmarshal(this.Ctx.Input.RequestBody, &email)
	id, _ := strconv.ParseInt(Id,10,64)
	email.Id = id
	data,err := email.UpdateEmail()
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	this.ajaxMsg("ok",MSG_OK,data)
}

// @Title 删除一个邮件服务器的信息
// @Description 删除一个邮件服务器的信息
// @Param	id		path 	string	true		"邮件服务器的ID"
// @Success 0 {string} ok
// @Failure -1 删除失败
// @router /:id [delete]
func (this *EmailController) Delete() {
	Id := this.Ctx.Input.Param(":id")
	if Id == "" {
		this.ajaxMsg("id不能为空",MSG_ERR,"")
	}
	var email models.Email
	id, _ := strconv.ParseInt(Id,10,64)
	email.Id = id
	err := email.DeleteEmail()
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	this.ajaxMsg("ok",MSG_OK,"")

}


// @Title 发送邮件
// @Description 发送邮件
// @Param	body		body 	models.SendEmail	true		"发送邮件的内容：To:发送给谁，多个邮箱用英文逗号分割 Subject:邮件主题 Body:邮件内容，可以html格式,不能超过15k"
// @Param	id		path 	string	true		"发送使用邮件服务器的ID"
// @Success 0 {string} ok
// @Failure -1 参数错误,具体看message
// @router /send/:id [post]
func (this *EmailController) Send() {
	Id := this.Ctx.Input.Param(":id")
	if Id == "" {
		this.ajaxMsg("id不能为空",MSG_ERR,"")
	}
	var email models.Email
	id, _ := strconv.ParseInt(Id,10,64)
	email.Id = id
	err := email.GetInfo()
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	var sendEmail models.SendEmail
	json.Unmarshal(this.Ctx.Input.RequestBody, &sendEmail)
	if email.IsSsl == 1 {
		err = libs.SendEmailSSL(email.UserName,sendEmail.To,email.Password,email.HostName,email.NickName,sendEmail.Subject,sendEmail.Body,strconv.Itoa(email.Port))
	} else {
		err = libs.SendEmail(email.UserName,sendEmail.To,email.Password,email.HostName,email.NickName,sendEmail.Subject,sendEmail.Body,strconv.Itoa(email.Port))
	}
	if err != nil {
		this.ajaxMsg(err.Error(),MSG_ERR,"")
	}
	this.ajaxMsg("ok",MSG_OK,"")
}


